from models.model_manager import ModelManager
from config import MODEL_CONFIG
from fastapi import Depends, Request
from app.app_main import get_app_state
import asyncio

# Initialize model manager
model_manager = ModelManager()

async def get_dependencies(request: Request = None):
    """
    Provide dependencies for routes: model_manager, config, and app_state.
    """
    # 如果request存在且有state属性，优先使用request.state
    # 否则通过get_app_state()获取全局应用状态
    app_state = None
    if request and hasattr(request, 'state'):
        app_state = request.app.state
    
    # 如果app_state为None或没有必要的属性，重新获取
    if app_state is None or not hasattr(app_state, 'ollama_client'):
        app_state = await get_app_state()
    
    return {
        "model_manager": model_manager,
        "config": MODEL_CONFIG,
        "app_state": app_state
    }